$(function(){
	new ehaoyao.component.Slideshow('#fade_layout2', "fade");
	var url = window.location.href;
	if (url.indexOf('/integral/index') != -1) {
		$('#index').addClass('cur');
	} else if(url.indexOf('/integral/vip/index') != -1) {
		$('#vip_index').addClass('cur');
	}
    
    //划过兑换排行榜效果
    $('#rank').find('li').on({
         mouseenter: function(){
            $(this).addClass('cur').siblings('li').removeClass('cur');
        }
    });
    //划过商品效果
    $('#product').find('li').on({
        mouseenter: function(){
            $(this).addClass('hover');
        },
        mouseleave: function(){
            $(this).removeClass('hover');
        }
    });
    $('.js-sub').click(function(){
		calculate(this, 'sub');
	});
	//加减商品数量
	$('.js-add').click(function(){
		calculate(this, 'add');
	});
	var calculate = function( elem, type ){
		var _max = 9999,
			_input = $(elem).parent().siblings('.js-input'),
			_count = parseInt( _input.val() );
        _input.css("color", "#34495e");
		if( type == 'add'){
			if( _count < _max ){
				_count++;
				_input.val( _count );
			};
		}else{
			if( _count >= 2 ){
				_count--;
				_input.val( _count );
			};
		};
	};
});


function PageList(){
	var isVip = $('#isVip').val();
	var prevPage_input = $('#prevPage');
	var nextPage_input = $('#nextPage');
	var pagination_btn_prev = $('.pagination-btn .prev');
	var pagination_btn_next = $('.pagination-btn .next');
	var pagination_page = $('.pagination .first_end_page');
	var down_input = $('#down_input');
	var up_input = $('#up_input');
//	var api = new Api();
	var _params = {};
	var _contentId = "";

	//翻页列表初始化
	this.initPageList = function(params , contentId){
		_params = params;
		_contentId = contentId;
		if(contentId=="insurance"){
			setInsurancePageList(_params , _contentId);
		}else{
			setPageList(_params , _contentId);
		}
		setPageTurnEvent();
	};
	
	//列表初始化
	this.initList = function(params , contentId){
		_params = params;
		_contentId = contentId;
		if(contentId=="insurance"){
			setInsuranceList(_params , _contentId);
		}else{
			setList(_params , _contentId);
		}
	};
	
	//列表
	var setList = function (params , contentId) {
		var api = new Api();
		api.localAjaxForJson(api.localIntegralCouponList, params, function(json) {
			var ul_product = $('#'+contentId);
			if (json.couponList) {
				var ul = "";
				for (var i = 0; i < json.couponList.length; i++) {
					var o = json.couponList[i];
					var li = '<li>'
		            + '<p><img src="'+o.imgUrl+'" /></p>'
		            + '<p class="summary"><span class="fl"><i class="icon-name"></i><a href="javascript:;" title="'+o.title+'"><font color="#bbbbbb">' + (o.title.length > 7 ? o.title.substring(0,7)+'...':o.title) + '</font></a> '+o.reduceValue+'元券</span></p>'
		            + '<p class="info"><span class="fl"><i class="icon-integral"></i><em>' + o.integralNum + '</em>分</span><span class="fr"><a href="javascript:void(0);" _title="'+o.title+'" _id="'+o.id+'" _integralNum="'+o.integralNum+'" class="js-exchange">立即兑换</a></span></p>';
					var div = "";
		            if (o.levelId == 20001)
		            	div = '<div class="vip vip-1"></div>';
		            if (o.levelId == 20002)
		            	div = '<div class="vip vip-2"></div>';
		            if (o.levelId == 20003)
		            	div = '<div class="vip vip-3"></div>';
		        	li = li + div + '</li>';
		        	ul = ul + li;
				}
				if (ul == '') {
					ul = '<li>暂无优惠券</li>';
				}
				ul_product.html(ul);
				setExchangeEvent();
			} else {
				ul_product.html('<li>暂无优惠券</li>');
			}
		}, "get");
	};
	//保险列表
	var setInsuranceList = function (params , contentId) {
		var api = new Api();
		api.localAjaxForJson(api.localIntegralInsuranceList, params, function(json) {
			var ul_product = $('#'+contentId);
			if (json.insuranceList) {
				var ul = "";
				for (var i = 0; i < json.insuranceList.length; i++) {
					var o = json.insuranceList[i];
					var li = '<li>'
						+ '<p><img src="'+o.imgUrl+'" /></p>'
						+ '<p class="summary"><span class="fl"><i class="icon-name"></i>' + o.title + '</span></p>'
						+ '<p class="info"><span class="fl"><i class="icon-integral"></i><em>' + o.integralNum + '</em>分</span><span class="fr"><a href="javascript:void(0);" _title="'+o.title+'" _id="'+o.id+'" _integralNum="'+o.integralNum+'" class="insurance-exchange">立即兑换</a></span></p>';
					var div = "";
					if (o.levelId == 20001)
						div = '<div class="vip vip-1"></div>';
					if (o.levelId == 20002)
						div = '<div class="vip vip-2"></div>';
					if (o.levelId == 20003)
						div = '<div class="vip vip-3"></div>';
					li = li + div + '</li>';
					ul = ul + li;
				}
				if (ul == '') {
					ul = '<li>暂无保险</li>';
				}
				ul_product.html(ul);
				setInsuranceExchangeEvent();
			} else {
				ul_product.html('<li>暂无保险</li>');
			}
		}, "get");
	};
	
	//翻页列表
	var setPageList = function (params,contentId) {
		if(contentId=="insurance"){
			setInsurancePageList(params,contentId);
		}else{
			setCouponPageList(params,contentId);
		}

	};
	
	function setCouponPageList(params,contentId){
		var api = new Api();
		api.localAjaxForJson(api.localIntegralCouponList, params, function(json) {
			var ul_product = $('#'+contentId);
			ul_product.empty();
			pagination_page.empty();
			if (json.couponList) {
				var ul = "";
				for (var i = 0; i < json.couponList.length; i++) {
					var o = json.couponList[i];
					var li = '<li>'
		            + '<p><img src="'+o.imgUrl+'" /></p>'
		            + '<p class="summary"><span class="fl"><i class="icon-name"></i><a href="javascript:;" title="'+o.title+'"><font color="#bbbbbb">' + (o.title.length > 7 ? o.title.substring(0,7)+'...':o.title) + '</font></a> '+o.reduceValue+'元券</span></p>'
		            + '<p class="info"><span class="fl"><i class="icon-integral"></i><em>' + o.integralNum + '</em>分</span><span class="fr"><a href="javascript:void(0);" _title="'+o.title+'" _id="'+o.id+'" _integralNum="'+o.integralNum+'" class="js-exchange">立即兑换</a></span></p>';
					var div = "";
		            if (o.levelId == 20001)
		            	div = '<div class="vip vip-1"></div>';
		            if (o.levelId == 20002)
		            	div = '<div class="vip vip-2"></div>';
		            if (o.levelId == 20003)
		            	div = '<div class="vip vip-3"></div>';
		        	li = li + div + '</li>';
		        	ul = ul + li;
				}
				if (ul == '') {
					ul = '<li>暂无优惠券</li>';
				}
				ul_product.html(ul);
				setExchangeEvent();
				var pageTurn = json.pageTurn;
				setPageTurn(pageTurn);
			} else {
				ul_product.html('<li>暂无优惠券</li>');
			}
		}, "get");
	}
	
	function setInsurancePageList(params,contentId){
		var api = new Api();
		api.localAjaxForJson(api.localIntegralInsuranceList, params, function(json) {
			var ul_product = $('#'+contentId);
			ul_product.empty();
			pagination_page.empty();
			if (json.insuranceList) {
				var ul = "";
				for (var i = 0; i < json.insuranceList.length; i++) {
					var o = json.insuranceList[i];
					var li = '<li>'
						+ '<p><img src="'+o.imgUrl+'" /></p>'
						+ '<p class="summary"><span class="fl"><i class="icon-name"></i>' + o.title + '</span></p>'
						+ '<p class="info"><span class="fl"><i class="icon-integral"></i><em>' + o.integralNum + '</em>分</span><span class="fr"><a href="javascript:void(0);" _title="'+o.title+'" _id="'+o.id+'" _integralNum="'+o.integralNum+'" class="insurance-exchange">立即兑换</a></span></p>';
					var div = "";
					if (o.levelId == 20001)
						div = '<div class="vip vip-1"></div>';
					if (o.levelId == 20002)
						div = '<div class="vip vip-2"></div>';
					if (o.levelId == 20003)
						div = '<div class="vip vip-3"></div>';
					li = li + div + '</li>';
					ul = ul + li;
				}
				if (ul == '') {
					ul = '<li>暂无保险</li>';
				}
				ul_product.html(ul);
				setInsuranceExchangeEvent();
				var pageTurn = json.pageTurn;
				setPageTurn(pageTurn);
			} else {
				ul_product.html('<li>暂无保险</li>');
			}
		}, "get");
	}
	
	
	
	
	
	var setInsurancePageList = function (params,contentId) {
		var api = new Api();
		api.localAjaxForJson(api.localIntegralInsuranceList, params, function(json) {
			var ul_product = $('#'+contentId);
			ul_product.empty();
			pagination_page.empty();
			if (json.insuranceList) {
				var ul = "";
				for (var i = 0; i < json.insuranceList.length; i++) {
					var o = json.insuranceList[i];
					var li = '<li>'
						+ '<p><img src="'+o.imgUrl+'" /></p>'
						+ '<p class="summary"><span class="fl"><i class="icon-name"></i><a href="javascript:;" title="'+o.title+'"><font color="#bbbbbb">' + (o.title.length > 7 ? o.title.substring(0,7)+'...':o.title) + '</font></a></span></p>'
						+ '<p class="info"><span class="fl"><i class="icon-integral"></i><em>' + o.integralNum + '</em>分</span><span class="fr"><a href="javascript:void(0);" _title="'+o.title+'" _id="'+o.id+'" _integralNum="'+o.integralNum+'" class="js-exchange">立即兑换</a></span></p>';
					var div = "";
					if (o.levelId == 20001)
						div = '<div class="vip vip-1"></div>';
					if (o.levelId == 20002)
						div = '<div class="vip vip-2"></div>';
					if (o.levelId == 20003)
						div = '<div class="vip vip-3"></div>';
					li = li + div + '</li>';
					ul = ul + li;
				}
				if (ul == '') {
					ul = '<li>暂无保险</li>';
				}
				ul_product.html(ul);
				setInsuranceExchangeEvent();
				var pageTurn = json.pageTurn;
				setPageTurn(pageTurn);
			} else {
				ul_product.html('<li>暂无保险</li>');
			}
		}, "get");
	};
	
	//翻页赋值
	var setPageTurn = function (pageTurn) {
		var prevPage = pageTurn.prevPage;
		var currentPage = pageTurn.currentPage;
		var nextPage = pageTurn.nextPage;
		var pageCount = pageTurn.pageCount;
		var firstPage = pageTurn.firstPage;
		up_input.attr('max', pageCount);
		up_input.val(currentPage);
		down_input.attr('max', pageCount);
		down_input.val(currentPage);
		if (firstPage == pageCount) {
			pagination_page.html('<em>'+pageCount+'</em>');
			pagination_btn_prev.addClass('end');
			pagination_btn_next.addClass('end');
		} else {
			pagination_page.html(currentPage+'/<em>'+pageCount+'</em>');
			pagination_btn_next.removeClass('end');
			pagination_btn_prev.removeClass('end');
			
			if (currentPage == firstPage) {
				pagination_btn_prev.addClass('end');
			}
			if (currentPage == pageCount) {
				pagination_btn_next.addClass('end');
			}
		}
		
		prevPage_input.val(prevPage);
		nextPage_input.val(nextPage);
		
	};

	//翻页事件
	var setPageTurnEvent = function(){
		up_input.keyup(function(event) {
			if(event.keyCode == 13) {
				_params.pageNo = up_input.val();
				setPageList(_params , _contentId);
			}
		});
		down_input.keyup(function(event) {
			if(event.keyCode == 13) {
				_params.pageNo = down_input.val();
				setPageList(_params , _contentId);
			}
		});
		pagination_btn_prev.click(function() {
			if (!pagination_btn_prev.hasClass('end')) {
				_params.pageNo = prevPage_input.val();
				setPageList(_params , _contentId);
			}
		});
		pagination_btn_next.click(function() {
			if (!pagination_btn_next.hasClass('end')) {
				_params.pageNo = nextPage_input.val();
				setPageList(_params , _contentId);
			}
		});
	};
}

function setExchangeEvent() {
	var api = new Api();
	$('.js-exchange').click(function(){
		var integralNum = $(this).attr('_integralNum');
		var id = $(this).attr('_id');
		var title = $(this).attr('_title');
	    var popHtml = '<div class="hd">提示</div>'+
	                  '<div class="content">'+
	                  '<div class="exchange-bd">'+
	                  '<div class="js-confirm">'+
	                  '<p class="title">是否兑换优惠券 '+title+'？</p>'+
	                  '<p class="summary">兑换该优惠券将消耗<em class="num"> '+integralNum+' </em>积分</p>'+
	                  '<div class="exchange-btns">'+
	                  '<a id="getExchange" href="javascript:void(0)" class="submit-btn-green">是</a>'+
	                  '<a href="javascript:void(0);" class="submit-btn-red js-cancel">否</a>'+
	                  '</div></div><div class="js-success hide">'+
	                  '<p class="title">优惠券已兑换成功！</p>'+
	                  '<div class="exchange-btns">'+
	                  '<a href="javascript:void(0)" class="submit-btn-green js-cancel">继续兑换</a>'+
	                  '<a href="http://ucenter.ehaoyao.com/coupon/index.html" class="submit-btn-red" target="_blank">查看优惠券</a>'+
	                  '</div></div><div class="js-fail hide">'+
	                  '<p class="title">优惠券兑换失败，请重新兑换！</p>'+
	                  '<div class="exchange-btns">'+
	                  '<a href="javascript:void(0)" class="submit-btn-green js-cancel">确定</a>'+
	                  '</div></div></div></div>';
	    new ehaoyao.component.Tips({           
	        id: "integralExchange",                                     
	        style: 'integral-exchange-pops hy-global',                                  
	        target: $("#body"),                                       
	        close_btn: true,                               
	        content: popHtml,       
	        position: "center",
	        correction: {top:0,left:0},                  
	        width: 471,                                                                          
	        scroll: false,
	        mod: true,
	        stay: true,
	        callback:function(fn){
	            $('#getExchange').click(function(){
	            	var params = {
            			"id" : id
            		};
	            	api.localAjaxForJson(api.localIntegralExchangeCoupon, params, function(json) {
	            		if (json.code == 1) {
	            			//兑换成功
	    	                $('#integralExchange').addClass('integral-exchange-pops-success');
	    	                $('.js-success').removeClass('hide');
	    	                $('.js-confirm, .js-fail').addClass('hide');
	            		} else {
	            			//兑换失败
	    	                $('.js-fail').removeClass('hide');
	    	                $('.js-confirm, .js-success').addClass('hide');
	    	                $('.js-fail .title').empty();
	    	                $('.js-fail .title').html(json.tip.replace("请求处理失败：", "兑换失败："));
	            		}
	            	}, "get");
	            });
	            $('.js-cancel').click(fn);
	        }
	    });
	});
};
function setInsuranceExchangeEvent() {
	var api = new Api();
	$('.insurance-exchange').click(function(){
		var integralNum = $(this).attr('_integralNum');
		var id = $(this).attr('_id');
		var title = $(this).attr('_title');
		var popHtml = '<div class="hd">提示</div>'+
		'<div class="content">'+
		'<div class="exchange-bd">'+
		'<div class="js-confirm">'+
		'<p class="title">是否兑换保险 '+title+'？</p>'+
		'<p class="summary">兑换该保险将消耗<em class="num"> '+integralNum+' </em>积分</p>'+
		'<div class="exchange-btns">'+
		'<a id="getExchange" href="javascript:void(0)" class="submit-btn-green">是</a>'+
		'<a href="javascript:void(0);" class="submit-btn-red js-cancel">否</a>'+
		'</div></div><div class="js-success hide">'+
		'<p class="title">保险已兑换成功！请查看站内短信获取激活码！</p>'+
		'<div class="exchange-btns">'+
		'<a href="javascript:void(0)" class="submit-btn-green js-cancel">继续兑换</a>'+
		'<a href="http://ucenter.ehaoyao.com/messageDm/index.html" class="submit-btn-red" target="_blank">查看激活码</a>'+
		'</div></div><div class="js-fail hide">'+
		'<p class="title">保险兑换失败，请重新兑换！</p>'+
		'<div class="exchange-btns">'+
		'<a href="javascript:void(0)" class="submit-btn-green js-cancel">确定</a>'+
		'</div></div></div></div>';
		new ehaoyao.component.Tips({           
			id: "integralExchange",                                     
			style: 'integral-exchange-pops hy-global',                                  
			target: $("#body"),                                       
			close_btn: true,                               
			content: popHtml,       
			position: "center",
			correction: {top:0,left:0},                  
			width: 471,                                                                          
			scroll: false,
			mod: true,
			stay: true,
			callback:function(fn){
				$('#getExchange').click(function(){
					var params = {
							"id" : id
					};
					api.localAjaxForJson(api.localIntegralExchangeCdkey, params, function(json) {
						if (json.code == 1) {
							//兑换成功
							$('#integralExchange').addClass('integral-exchange-pops-success');
							$('.js-success').removeClass('hide');
							$('.js-confirm, .js-fail').addClass('hide');
						} else {
							//兑换失败
							$('.js-fail').removeClass('hide');
							$('.js-confirm, .js-success').addClass('hide');
							$('.js-fail .title').empty();
							$('.js-fail .title').html(json.tip.replace("请求处理失败：", "兑换失败："));
						}
					}, "get");
				});
				$('.js-cancel').click(fn);
			}
		});
	});
};




